﻿using System;
using System.Web;
using System.Globalization;

namespace Hanhud.Web.Config
{
    [Serializable()]
    public class CSiteConfig : Core.CBaseConfig
    {
        public bool EnableCaching { get; set; }
        public int CacheDuration { get; set; }
        public string PrefixCacheString { get; set; }
        public string BaseUrl { get; set; }
        public string Theme { get; set; }
        public string PageTitle { get; set; }
        public string AuthenticatedUserSessionName { get; set; }
        public string AuthenticationCookieName { get; set; }
        public string WebSettingsCookieName { get; set; }
        public int CookieExpireDays { get; set; }
        public int DefaultPageSize { get; set; }
        public bool EnableContactFromProduct { get; set; }
        public CultureInfo VietnamCulture { get; private set; }
        
        public IUrlBuilder UrlBuilder
        {
            get
            {
                if (HttpContext.Current.Application["UrlBuilder"] == null)
                    HttpContext.Current.Application["UrlBuilder"] = Activator.CreateInstance(typeof(CUrlBuilder));
                return (IUrlBuilder)HttpContext.Current.Application["UrlBuilder"];
            }
        }

        private static CSiteConfig _current;
        public static new CSiteConfig Current
        {
            get
            {
                if (_current == null)
                    _current = new CSiteConfig();
                return _current;
            }
        }

        private CSiteConfig()
        {
            this.ConnectionStringName = "HanhudConnectionString";
            this.CookieExpireDays = 7;
            this.EnableCaching = true;
            this.CacheDuration = 600;
            this.PrefixCacheString = "Hanhud_";
            this.BaseUrl = "~/Hanhud";
            this.AppRoot = "~/";
            this.AppResourceAssembly = "Hanhud.Web.Resources, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f8a40f2cd70333bf";
            this.Theme = "Default";
            this.PageTitle = "Tự động hóa";
            this.AuthenticationCookieName = ".HANHUDSecurity";
            this.AuthenticatedUserSessionName = "AuthenticatedUser";
            this.DefaultPageSize = 7;
            this.WebSettingsCookieName = ".HANHUDWebSettings";
            this.EnableContactFromProduct = false;
            this.VietnamCulture = System.Globalization.CultureInfo.CreateSpecificCulture("vi-VN");
        }

        public string[] GetThemes()
        {
            if (HttpContext.Current.Cache["SiteThemes"] != null)
            {
                return (string[])HttpContext.Current.Cache["SiteThemes"];
            }
            else
            {
                string themesDirPath = HttpContext.Current.Server.MapPath("~/App_Themes");
                // get the array of themes folders under /app_themes
                string[] themes = System.IO.Directory.GetDirectories(themesDirPath);
                for (int i = 0; i <= themes.Length - 1; i++)
                    themes[i] = System.IO.Path.GetFileName(themes[i]);
                // cache the array with a dependency to the folder
                System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(themesDirPath);
                HttpContext.Current.Cache.Insert("SiteThemes", themes, dep);
                return themes;
            }
        }

        public void SetTheme(string name)
        {

        }
    }
}
